package com.itheima.app.controller;

import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    //保存用户
    @PostMapping("/user/save")
    public ResponseEntity save(@RequestBody User user){
        return  userManager.save(user);
    }

    //根据手机查找用户
    @GetMapping("/user/findByPhone")
    public ResponseEntity findByPhone(String phone){
        return userManager.findByPhone(phone);
    }

    //发送手机短信
    @PostMapping("/user/login")
    public void sendSms(@RequestBody Map<String,String> param){
        //获取手机号
        String phone = param.get("phone");
        //调用UserManger发送
           userManager.sendSms(phone);
    }

    //注册 or 登录
    @PostMapping("/user/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> param){
        String phone = param.get("phone");
        String verificationCode = param.get("verificationCode");
      return   userManager.loginVerification(phone,verificationCode);
    }

    //完善用户信息
    @PostMapping("/user/loginReginfo")
    public void loginReginfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        //调用manage
        userManager.loginReginfo(userInfo,token);

    }

    //完善用户头像
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto,
                                            @RequestHeader("Authorization")String token) throws IOException {
        return  userManager.loginReginfoHead(headPhoto,token);
    }
    //查询用户信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoVo(Long userID,Long huanxinID,@RequestHeader("Authorization")String token){
       if (userID!=null){
           return userManager.findUserInfoVo(userID);
       }else if (huanxinID!=null){
           return userManager.findUserInfoVo(huanxinID);
       }else {
           User user = userManager.findUserByToken(token);
           return userManager.findUserInfoVo(user.getId());
       }
    }

    //修改用户信息
    @PutMapping("/users")
    public void  updateUserInfoVo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization")String token){
         userManager.updateUserInfoVo(userInfo,token);
    }

    //查询探花
      @GetMapping("/tanhua/cards")
    public ResponseEntity showTanhua(){
     return    userManager.showTanhua();
    }

}


